home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Matrix3D.java < prev    next >
Text File  |  1998-09-15  |  6KB  |  226 lines

  1. /*
  2.  * @(#)Matrix3D.java    1.4 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. /** A fairly conventional 3D matrix object that can transform sets of
  32.     3D points and perform a variety of manipulations on the transform */
  33. class Matrix3D {
  34.     float xx, xy, xz, xo;
  35.     float yx, yy, yz, yo;
  36.     float zx, zy, zz, zo;
  37.     static final double pi = 3.14159265;
  38.     /** Create a new unit matrix */
  39.     Matrix3D () {
  40.     xx = 1.0f;
  41.     yy = 1.0f;
  42.     zz = 1.0f;
  43.     }
  44.     /** Scale by f in all dimensions */
  45.     void scale(float f) {
  46.     xx *= f;
  47.     xy *= f;
  48.     xz *= f;
  49.     xo *= f;
  50.     yx *= f;
  51.     yy *= f;
  52.     yz *= f;
  53.     yo *= f;
  54.     zx *= f;
  55.     zy *= f;
  56.     zz *= f;
  57.     zo *= f;
  58.     }
  59.     /** Scale along each axis independently */
  60.     void scale(float xf, float yf, float zf) {
  61.     xx *= xf;
  62.     xy *= xf;
  63.     xz *= xf;
  64.     xo *= xf;
  65.     yx *= yf;
  66.     yy *= yf;
  67.     yz *= yf;
  68.     yo *= yf;
  69.     zx *= zf;
  70.     zy *= zf;
  71.     zz *= zf;
  72.     zo *= zf;
  73.     }
  74.     /** Translate the origin */
  75.     void translate(float x, float y, float z) {
  76.     xo += x;
  77.     yo += y;
  78.     zo += z;
  79.     }
  80.     /** rotate theta degrees about the y axis */
  81.     void yrot(double theta) {
  82.     theta *= (pi / 180);
  83.     double ct = Math.cos(theta);
  84.     double st = Math.sin(theta);
  85.  
  86.     float Nxx = (float) (xx * ct + zx * st);
  87.     float Nxy = (float) (xy * ct + zy * st);
  88.     float Nxz = (float) (xz * ct + zz * st);
  89.     float Nxo = (float) (xo * ct + zo * st);
  90.  
  91.     float Nzx = (float) (zx * ct - xx * st);
  92.     float Nzy = (float) (zy * ct - xy * st);
  93.     float Nzz = (float) (zz * ct - xz * st);
  94.     float Nzo = (float) (zo * ct - xo * st);
  95.  
  96.     xo = Nxo;
  97.     xx = Nxx;
  98.     xy = Nxy;
  99.     xz = Nxz;
  100.     zo = Nzo;
  101.     zx = Nzx;
  102.     zy = Nzy;
  103.     zz = Nzz;
  104.     }
  105.     /** rotate theta degrees about the x axis */
  106.     void xrot(double theta) {
  107.     theta *= (pi / 180);
  108.     double ct = Math.cos(theta);
  109.     double st = Math.sin(theta);
  110.  
  111.     float Nyx = (float) (yx * ct + zx * st);
  112.     float Nyy = (float) (yy * ct + zy * st);
  113.     float Nyz = (float) (yz * ct + zz * st);
  114.     float Nyo = (float) (yo * ct + zo * st);
  115.  
  116.     float Nzx = (float) (zx * ct - yx * st);
  117.     float Nzy = (float) (zy * ct - yy * st);
  118.     float Nzz = (float) (zz * ct - yz * st);
  119.     float Nzo = (float) (zo * ct - yo * st);
  120.  
  121.     yo = Nyo;
  122.     yx = Nyx;
  123.     yy = Nyy;
  124.     yz = Nyz;
  125.     zo = Nzo;
  126.     zx = Nzx;
  127.     zy = Nzy;
  128.     zz = Nzz;
  129.     }
  130.     /** rotate theta degrees about the z axis */
  131.     void zrot(double theta) {
  132.     theta *= (pi / 180);
  133.     double ct = Math.cos(theta);
  134.     double st = Math.sin(theta);
  135.  
  136.     float Nyx = (float) (yx * ct + xx * st);
  137.     float Nyy = (float) (yy * ct + xy * st);
  138.     float Nyz = (float) (yz * ct + xz * st);
  139.     float Nyo = (float) (yo * ct + xo * st);
  140.  
  141.     float Nxx = (float) (xx * ct - yx * st);
  142.     float Nxy = (float) (xy * ct - yy * st);
  143.     float Nxz = (float) (xz * ct - yz * st);
  144.     float Nxo = (float) (xo * ct - yo * st);
  145.  
  146.     yo = Nyo;
  147.     yx = Nyx;
  148.     yy = Nyy;
  149.     yz = Nyz;
  150.     xo = Nxo;
  151.     xx = Nxx;
  152.     xy = Nxy;
  153.     xz = Nxz;
  154.     }
  155.     /** Multiply this matrix by a second: M = M*R */
  156.     void mult(Matrix3D rhs) {
  157.     float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;
  158.     float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;
  159.     float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;
  160.     float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;
  161.  
  162.     float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;
  163.     float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;
  164.     float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;
  165.     float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;
  166.  
  167.     float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;
  168.     float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;
  169.     float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;
  170.     float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;
  171.  
  172.     xx = lxx;
  173.     xy = lxy;
  174.     xz = lxz;
  175.     xo = lxo;
  176.  
  177.     yx = lyx;
  178.     yy = lyy;
  179.     yz = lyz;
  180.     yo = lyo;
  181.  
  182.     zx = lzx;
  183.     zy = lzy;
  184.     zz = lzz;
  185.     zo = lzo;
  186.     }
  187.  
  188.     /** Reinitialize to the unit matrix */
  189.     void unit() {
  190.     xo = 0;
  191.     xx = 1;
  192.     xy = 0;
  193.     xz = 0;
  194.     yo = 0;
  195.     yx = 0;
  196.     yy = 1;
  197.     yz = 0;
  198.     zo = 0;
  199.     zx = 0;
  200.     zy = 0;
  201.     zz = 1;
  202.     }
  203.     /** Transform nvert points from v into tv.  v contains the input
  204.         coordinates in floating point.  Three successive entries in
  205.     the array constitute a point.  tv ends up holding the transformed
  206.     points as integers; three successive entries per point */
  207.     void transform(float v[], int tv[], int nvert) {
  208.     float lxx = xx, lxy = xy, lxz = xz, lxo = xo;
  209.     float lyx = yx, lyy = yy, lyz = yz, lyo = yo;
  210.     float lzx = zx, lzy = zy, lzz = zz, lzo = zo;
  211.     for (int i = nvert * 3; (i -= 3) >= 0;) {
  212.         float x = v[i];
  213.         float y = v[i + 1];
  214.         float z = v[i + 2];
  215.         tv[i    ] = (int) (x * lxx + y * lxy + z * lxz + lxo);
  216.         tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);
  217.         tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);
  218.     }
  219.     }
  220.     public String toString() {
  221.     return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"
  222.         + yo + "," + yx + "," + yy + "," + yz + ";"
  223.         + zo + "," + zx + "," + zy + "," + zz + "]");
  224.     }
  225. }
  226.